import { Body, Controller, Post, Res, Headers } from '@nestjs/common';
import { ApiOkResponse, ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';
import { ADMIN_PREFIX } from '../../admin.constants';
import { ReportFormShiftReportStatisticsService } from './shiftReportStatistics.service';
import i18n from '@main/i18n';
import { format } from 'date-fns';

@ApiSecurity(ADMIN_PREFIX)
@ApiTags('班组报工统计模块')
@Controller('shiftReportStatistics')
export class ReportFormShiftReportStatisticsController {
  constructor(private shiftReportStatisticsService: ReportFormShiftReportStatisticsService) {}

  @ApiOperation({ summary: '获取班组报工统计列表' })
  @ApiOkResponse()
  @Post('page')
  async page(@Body() dto) {
    const [list, total] = await this.shiftReportStatisticsService.page(dto);

    return {
      list,
      pagination: {
        total,
        pageNum: dto.pageNum,
        pageSize: dto.pageSize,
      },
    };
  }

  @ApiOperation({ summary: '导出班组报工统计列表' })
  @ApiOkResponse()
  @Post('export')
  async export(@Body() dto, @Res() res, @Headers('language') languageType) {
    const buffer = await this.shiftReportStatisticsService.export(dto, languageType);

    const langObj = i18n[languageType]['common']['table'];

    res.header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

    res.header(
      'Content-Disposition',
      `attachment; filename=${encodeURIComponent(
        langObj['shiftReportStatistics'] + format(new Date(), 'yyyyMMddHHmmss') + '.xlsx'
      )}`
    );

    res.header('Access-Control-Expose-Headers', 'Content-Disposition');

    res.send(buffer);
  }
}
